package com.burst.reptile.modular.zp.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.burst.reptile.common.constant.UserConstants;
import com.burst.reptile.common.exception.CustomException;
import com.burst.reptile.common.utils.ToolUtil;
import com.burst.reptile.modular.zp.entity.ZpFormat;
import com.burst.reptile.modular.zp.mapper.ZpFormatMapper;
import com.burst.reptile.modular.zp.service.IZpFormatService;

import cn.hutool.core.util.StrUtil;

/**
 * <p>
 * 业态列表 服务实现类
 * </p>
 *
 * @author wujin
 * @since 2020-06-17
 */
@Service
public class ZpFormatServiceImpl extends ServiceImpl<ZpFormatMapper, ZpFormat> implements IZpFormatService {

	@Override
	public List<ZpFormat> listAll(ZpFormat format) {
		QueryWrapper<ZpFormat> queryWrapper = new QueryWrapper<>();
		String formatName = format.getFormatName();
		if(StrUtil.isNotEmpty(formatName)) {
			queryWrapper.like("format_name", formatName);
		}
		return this.list(queryWrapper);
	}

	@Override
	public String checkFormatKeyUnique(ZpFormat format) {
		Long formatId = ToolUtil.isEmpty(format.getFormatId()) ? -1L : format.getFormatId();
		ZpFormat info = this.baseMapper.selectOne(new QueryWrapper<ZpFormat>().eq("format_name", format.getFormatName()));
		if (ToolUtil.isNotEmpty(info) && info.getFormatId().longValue() != formatId.longValue()) {
			return UserConstants.NOT_UNIQUE;
		}
		return UserConstants.UNIQUE;
	}

	@Override
	public int insertFormat(ZpFormat format) {
		return this.baseMapper.insert(format);
	}

	@Override
	public int updateFormat(ZpFormat format) {
		Integer formatId = format.getFormatId();
		if(ToolUtil.isEmpty(formatId)) {
			throw new CustomException("修改失败，缺少参数");
		}
		ZpFormat byId = this.getById(formatId);
		if(ToolUtil.isEmpty(byId)) {
			throw new CustomException("修改失败，资源不存在");
		}
		return this.baseMapper.updateById(format);
	}

	@Override
	public int deleteByIds(Integer[] ids) {
		return this.baseMapper.deleteColumnByIds(ids);
	}

}
